<template>
  <div class="bg">
    <!-- 时间搜索框 -->
    <searchTime
      @searchvalue="userSearch"
      @searchreset="searchreset"
      :options="options"
      :selectMsg="selectMsg"
      :titMsg="titMsg"
      :selectBoolen="false"
      :selectTime="true"
    />

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <el-col :span="2">
            <el-button type="success" @click="addmessage">新建推送</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="index" width="100" label="序号" align="center"></el-table-column>
        <el-table-column prop="messageTitle" label="推送标题" align="center"></el-table-column>
        <el-table-column prop="messageContent" label="推送内容" align="center"></el-table-column>
        <el-table-column prop="date" label="推送类型" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.messageType==1?'平台':scope.row.messageType==2?'充值':'订单'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="messageTime" label="推送时间" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="primary" size="small">查看</el-button>
            <el-button @click="deletMessage(scope.row)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        @current-change="currentpange"
        style="margin-top:24px"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import searchTime from "@/components/searchTime";
export default {
  components: {
    searchTime
  },
  data() {
    return {
      titMsg: "推送标题内容",
      selectMsg: "推送类型",
      options: [
        {
          value: 1,
          label: "系统推送"
        },
        {
          value: 2,
          label: "消息提醒"
        }
      ],
      value: "",
      value1: "",
      downloadLoading: false,
      tableData: [],
      total: 0,
      current: 1,
      // 模糊查询
      selectCode: "",
      startDate: "",
      endDate: ""
    };
  },
  mounted() {
    this.messagelist();
  },
  methods: {
    // 获取消息列表
    messagelist() {
      let param = {
        size: 10,
        current: this.current,
        selectCode: this.selectCode,
        startDate: this.startDate,
        endDate: this.endDate
      };
      this.$http.article.listPetMessage(param).then(res => {
        this.tableData = res.result.records;
        this.total = res.result.total;
        console.log("获取消息列表", res);
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 点击下一页分页
    currentpange(val) {
      this.current = val;
      this.messagelist();
    },
    // 删除消息提示
    deletMessage(val) {
      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.article
            .deleteMessage({ pet_messageId: val.messageId })
            .then(res => {
              if (res.code == 200) this.messagelist();
              /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

              //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
              const totalPage = Math.ceil((this.total - 1) / 10);

              console.log("totalPage", totalPage);

              //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
              const pagenum =
                this.current > totalPage ? totalPage : this.current;

              console.log("获取总条数", pagenum);

              //避免pagenum变为0
              this.current = pagenum < 1 ? 1 : pagenum;
              // 提示弹窗
              this.$message.success("删除成功!");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 搜索框
    userSearch(val) {
      this.current=1;
      this.selectCode = val.search;
      this.startDate = val.firstTime;
      this.endDate = val.endTime;
      this.messagelist();
      console.log("搜索框", val);
    },
    // 重置功能实现
    searchreset(val){
      if(val) {
        this.selectCode='',
        this.startDate='',
        this.endDate='',
        this.messagelist();
      }
      console.log('获取重置功能',val)
    },
    // 查看消息推送信息
    handleClick(val) {
      this.$router.push({
        path: "/home/addMessage",
        query: { messageId: val.messageId }
      });
    },
    // 新建消息推送
    addmessage() {
      this.$router.push("/home/addMessage");
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  padding-top: 20px;
}

.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>